package com.sannu.emc.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbManager {

	private String DbUsername = "root"; // TODO make it so that you can pass in
	// the user name and password and
	// database name maybe

	private String DbPassword = "sannu";

	private String DbName = "students";

	private String driverClassName = "com.mysql.jdbc.Driver";

	private String urlForCreatingDb = "jdbc:mysql://localhost:3306/students";

	private String urlForManipulatingDb = "jdbc:mysql://localhost:3306/"
			+ DbName;

	private Connection con;
	private PreparedStatement ps;
	

	public DbManager() {
		
		 try {
	            // load, and link the jdbc driver class
	            Class.forName(driverClassName);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
	
	 private Connection getCon() throws Exception {
		 
	        con = DriverManager.getConnection(urlForManipulatingDb,DbUsername, DbPassword);
	        // urlForManipulatingDb,DbUsername, DbPassword);
	        return con;
	    }
	 
	 

	public ResultSet select(String sqlQuery) {
		ResultSet res;
		try {
			ps = getCon().prepareStatement(sqlQuery);
			res = ps.executeQuery();
			/*while(res.next()){
				System.out.println(res.getString(2));
			}
			System.out.println("Success");*/
			return res;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(ps != null){
				try {
					ps.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					ps = null;
				}
			}
			if(con != null){
				try {
					con.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					con = null;
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return null;
	}

	public int insert(String sqlQuery) {
     
		return -1;
	}
	
	public int insert(PreparedStatement stmt) {
	     
		
		try {
			return stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return -1;
	}

	public int update(String sqlQuery) {

		return -1;
	}

	public int delete(String sqlQuery) {

		return -1;
	}

}


class Test {
	public static void main(String[] felight) {
		
		DbManager dbm = new DbManager();
		dbm.select("select * from Registration");
	}
}
